/** 数据统计 */
import { pageWaitHandleTask } from '@/api/waitHandle'
import { count } from '@/api/telRecord'
import { filtersRaw, pageRequsetBody } from '@/utils'
// const now = new Date()
// function setItem(key, v) {
//   const d = {
//     value: v,
//     time: new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59).getTime()
//   }
//   localStorage.setItem(key, JSON.stringify(d))
// }
// function getItem(key) {
//   let v = localStorage.getItem(key)
//   try {
//     v = JSON.parse(v)
//     if (parseInt(v.time) < new Date().getTime()) {
//       return 0
//     }
//     return parseInt(v.value)
//   } catch (error) {
//     return 0
//   }
// }
const state = {
  waitCount: 0, // 待办数量
  callInCount: 0, // 今日呼入量
  callOutCount: 0 // 今日呼出量
}

const mutations = {
  SET_WAIT_COUNT(state, v) {
    state.waitCount = v
  },
  SET_CALL_IN_COUNT(state, v) {
    state.callInCount = v || 0
  },
  SET_CALL_OUT_COUNT(state, v) {
    state.callOutCount = v || 0
  }
}

const getters = {
  waitCount: state => state.waitCount,
  callInCount: state => state.callInCount,
  callOutCount: state => state.callOutCount
}

const actions = {
  async getWaitCount({ commit, getters }) {
    if (!getters.accountNo) {
      return Promise.resolve()
    }
    const data = {
      onlyCountFlag: true,
      pageFlag: false,
      filtersRaw: [
        filtersRaw('state', 0, '='),
        filtersRaw('handler', getters.accountNo, '=')
      ]
    }
    return await pageWaitHandleTask(pageRequsetBody(data)).then(res => {
      commit('SET_WAIT_COUNT', res.data.total)
    })
  },
  async getCalldata({ commit }) {
    return await count().then(res => {
      commit('SET_CALL_IN_COUNT', res.data.callInCount)
      commit('SET_CALL_OUT_COUNT', res.data.callOutCount)
    })
  }
}

export default {
  state,
  mutations,
  actions,
  getters
}
